import {getUser} from '../user.js'
import db from '../mysql.js'

export function getCurrentUserMenuList(req, res) {
    getUser(req).then(user => {
		console.log('getUser', user)
		let sql =
			"select menu_id as menuId, menu_name as menuName, parent_id as parentId, order_num as orderNum, path, component, menu_type as menuType, status, perms, icon, create_by as createBy, create_time as createTime, update_by as updateBy, update_time as updateTime, remark from sys_menu "
		if (user.roleIds.split(',').indexOf('1') < 0) {
			sql += ' where menu_id in (select menu_id from sys_role_menu where FIND_IN_SET(role_id, ?))'
			db.query(sql, [user.roleIds], (err, result) => {
				console.log('/sys/menu/list', err, result)
				let results = {}
				if (err) {
					results.msg = err;
					results.code = 500
				} else {
					results.code = 200
					results.data = result;
				}
				res.json(results)
			})
		} else {
			db.query(sql, (err, result) => {
				console.log('/sys/menu/list - admin', err, result)
				let results = {}
				if (err) {
					results.msg = err;
					results.code = 500
				} else {
					results.code = 200
					results.data = result;
				}
				res.json(results)
			})
		}
		console.log('sql', sql)
	}).catch(err => {
		res.json(err)
	})
}